package com.cowain.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cowain.monitor.dao.ErpMoDao;
import com.cowain.monitor.dao.ErpSoDao;
import com.cowain.monitor.datasources.DataSourceNames;
import com.cowain.monitor.datasources.annotation.DataSource;
import com.cowain.monitor.entity.ErpMoEntity;
import com.cowain.monitor.entity.PmsProductionOrderEntity;
import com.cowain.monitor.service.ErpMoService;
import com.cowain.monitor.utils.PmsConstantUtils;
import com.cowain.monitor.utils.PmsProjectUtil;
import com.cowain.monitor.utils.R;
import com.cowain.monitor.utils.SubStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * PMS ehr-backend
 *
 * @Classname ErpMoServiceImpl
 * @Description TODO
 * @Date 2020-07-27 19:18
 * @Created 沈科举
 */
@Service
public class ErpMoServiceImpl extends ServiceImpl<ErpMoDao, ErpMoEntity> implements ErpMoService {
    @Autowired
    private ErpSoDao erpSoDao;

    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public List<ErpMoEntity> findErpMoByIsDel(Integer isdel) {
        int totalCount = baseMapper.findTotalCount();
        int totalPage=(totalCount/3000)+1;
        List<ErpMoEntity> erpMoEntityList=new ArrayList<>();
        for (int i = 1; i <=totalPage ; i++) {
            erpMoEntityList.addAll(baseMapper.findErpMoByIsDel(i));
        }
        return erpMoEntityList ;
    }

    @Override
    @DataSource(name = DataSourceNames.FIRST)
    public R addPmsProductionOrderEntity(List<ErpMoEntity> erpMoEntityList) {
        int result = 0;
        List<PmsProductionOrderEntity> list = new ArrayList<>();
        List<ErpMoEntity> updataErpMoList=new ArrayList<>();
        if (null != erpMoEntityList && 0 != erpMoEntityList.size()) {
            erpMoEntityList.forEach(e -> {

                PmsProductionOrderEntity p = new PmsProductionOrderEntity();
                int length = e.getProjectno().length();
                if (8 == length) {
                    String projectNo = SubStringUtils.jointProjectNo(e.getProjectno());
                    Long projectId = erpSoDao.findProjectNo(projectNo);

                    if (null != projectId) {
                        p.setProjectId(projectId);
                        p.setNumber(e.getNum().longValue());
                        p.setBatch(e.getDocno());
                        p.setNum(e.getNum().longValue());
                        p.setStatus(e.getState());
                        list.add(p);
                        updataErpMoList.add(e);

                    }
                }
            });
            if(null!=list&&0!=list.size()){
                result = baseMapper.addPmsProductionOrderEntity(list);
            }

        }
        return R.ok().put("result",result).put("updataErpMoList",updataErpMoList);
    }


    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public int updatErpMoById(List<ErpMoEntity> erpMoEntityList) {
        int result = 0;
        if (null != erpMoEntityList && 0 != erpMoEntityList.size()) {
            for (ErpMoEntity e:erpMoEntityList) {
                e.setIsdel(PmsConstantUtils.IS_DEL_1.intValue());
              result=baseMapper.updatErpMoById(e);
            }

        }
        return result;
    }

}
